package com.sanjiahantang.youjiaclient.base;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.support.multidex.MultiDex;

import com.liulishuo.filedownloader.FileDownloader;
import com.liulishuo.filedownloader.connection.FileDownloadUrlConnection;
import com.liulishuo.filedownloader.util.FileDownloadLog;
import com.orhanobut.logger.Logger;
import com.sanjiahantang.youjiaclient.config.ConfigValue;
import com.sockect3d.DensityUtil;
import com.umeng.socialize.PlatformConfig;
import com.umeng.socialize.UMShareAPI;

import java.io.File;
import java.net.Proxy;
import java.util.ArrayList;
import java.util.List;

import cn.jpush.android.api.JPushInterface;

//import com.tencent.bugly.Bugly;

/**
 * Created by Administrator on 2015/9/21.
 */
public class BaseApplication extends Application {

    {

        PlatformConfig.setWeixin(ConfigValue.WXAPP_ID, ConfigValue.WXAPP_SECRET);
    }

    private File extStorageAppBasePath;
    private File extStorageAppCachePath;

    @Override
    public void onCreate() {
        super.onCreate();
//        CrashReport.initCrashReport(getApplicationContext(), "88b3efd173", true);
//        Bugly.init(getApplicationContext(), "88b3efd173", false);

        UMShareAPI.get(this);

        initLogger();

        JPushInterface.setDebugMode(true);
        JPushInterface.init(this);

        FileDownloadLog.NEED_LOG = false;
        FileDownloader.setupOnApplicationOnCreate(this)
                .connectionCreator(new FileDownloadUrlConnection
                        .Creator(new FileDownloadUrlConnection.Configuration()
                        .connectTimeout(15_000) // set connection timeout.
                        .readTimeout(15_000) // set read timeout.
                        .proxy(Proxy.NO_PROXY) // set proxy
                ))
                .commit();

        DensityUtil.init(getApplicationContext());
    }

    private void initLogger() {
        Logger.init("tag");
    }

    @Override
    public File getCacheDir() {

        if (extStorageAppCachePath != null) {
            return extStorageAppCachePath;
        } else {
            return super.getCacheDir();
        }
    }

    @Override

    protected void attachBaseContext(Context base) {

        super.attachBaseContext(base);

        MultiDex.install(base);

    }

    private static List<Activity> lists = new ArrayList<>();

    public static void addActivity(Activity activity) {
        lists.add(0, activity);
    }

    public static void clearActivity() {
        if (lists != null) {
            for (Activity activity : lists) {
                activity.finish();
            }

            lists.clear();
        }
    }

}
